﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Models;
using System.Data;
using System.Data.SqlClient;
using DBUnitility;
namespace DAL
{
    public class PictureFileService
    {
        /// <summary>
        /// 根据项目工程的ID号获取图片
        /// </summary>
        /// <param name="id">工程ID</param>
        /// <returns></returns>
        public PictureFile GetPictureById(string id)
        {
            string sql = "select PictureId,StuImage,ID from PictureFile where ID =" + id;
            PictureFile objPictureFile = null;
            SqlDataReader objReader = SQLHelper.GetReader(sql);
            if (objReader.Read())
            {
                objPictureFile = new PictureFile() 
                {
                    PictureId = Convert.ToInt32(objReader["PictureId"]),
                    StuImage = objReader["StuImage"].ToString(),
                    ID = Convert.ToInt32(objReader["ID"])
                };
            }
            objReader.Close();
            return objPictureFile;
        }
        /// <summary>
        /// 添加图片
        /// </summary>
        /// <param name="objPictureFile"></param>
        /// <returns>返回图片的ID号</returns>
        public int AddPictureFile(PictureFile objPictureFile)
        {
            string sql = "insert into PictureFile(StuImage,ID) values('{0}',{1});select @@Identity";
            sql = string.Format(sql, objPictureFile.StuImage, objPictureFile.ID);
            return Convert.ToInt32(SQLHelper.GetSingalResult(sql));
        }
        /// <summary>
        /// 修改图片
        /// </summary>
        /// <param name="objPictureFile"></param>
        /// <returns></returns>
        public int UpdatePictureFile(PictureFile objPictureFile)
        {
            string sql = "update PictureFile set StuImage ='{0}',ID = {1} where ID = {2}"; 
            sql = string.Format(sql,objPictureFile.StuImage,objPictureFile.ID,objPictureFile.ID);
            return SQLHelper.Update(sql);
        }
        /// <summary>
        /// 删除图片
        /// </summary>
        /// <param name="pictureId"></param>
        /// <returns></returns>
        public int DeletePictureFile(string id)
        {
            //string sql = "delete from PictureFile where ID = " + id;
            string sql = "delete from PictureFile where 1 = 1 " ;
            if (id.Trim().Length != 0) sql += "and ID=" + id;
            return SQLHelper.Update(sql);
        }
        /// <summary>
        /// 检查该ID的图片是否已经存在
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public int IsExistsPicture(string id)
        {
            string sql = "select count(*) from PictureFile where ID = " + id;
            return Convert.ToInt32(SQLHelper.GetSingalResult(sql));
        }
    }
}
